<script lang="ts" setup>
import { Filter, Reading, Delete } from '@element-plus/icons-vue'
import { getTagTypeBySeverity } from '@/stores/style'
import { vp, form, detailDialog, selectedRow } from '@/composables/useWebscanState'
import { handleWebscanContextMenu } from '@/linkage/contextMenu'
import { RemovePocscanResult } from 'wailsjs/go/services/Database'
import { ElMessage } from 'element-plus'
import { dashboard } from '@/composables/useWebscanState'
import { taskManager } from '@/composables/useTaskManager'

async function deleteVuln(row: any) {
    let isSuccess = await RemovePocscanResult(form.taskId, row.ID, row.URL)
    if (!isSuccess) {
        ElMessage.error("删除失败")
        return
    }
    ElMessage.success({
        message: "删除成功",
        grouping: true,
    })
    const riskLevelKey = row.Severity as keyof typeof dashboard.riskLevel;
    if (dashboard.riskLevel[riskLevelKey] !== undefined) {
        dashboard.riskLevel[riskLevelKey]--;
    }
    vp.table.result = vp.table.result.filter(item => !(item.URL == row.URL && item.Severity == row.Severity && item.Name == row.Name));
    vp.ctrl.watchResultChange(vp.table);
    taskManager.updateTaskTable(form.taskId)
}
</script>

<template>
    <el-table :data="vp.table.pageContent" stripe height="100vh" 
        :highlight-current-row="true"
        :cell-style="{ textAlign: 'center' }"
        :header-cell-style="{ 'text-align': 'center' }"
        @sort-change="((data: any) => vp.ctrl.sortChange(data, true))"
        @row-contextmenu="handleWebscanContextMenu"
        @filter-change="vp.ctrl.filterChange">
        <el-table-column prop="ID" label="Template" width="250px" />
        <el-table-column prop="Type" label="Type" width="150px" />
        <el-table-column prop="Severity" width="150px" column-key="Severity" label="Severity"
            :filters="!form.runnningStatus ? vp.ctrl.getColumnFilters('Severity') : []" sortable="custom">
            <template #filter-icon>
                <Filter />
            </template>
            <template #default="scope">
                <el-tag :type="getTagTypeBySeverity(scope.row.Severity)"
                    :class="{ 'el-tag--critical': scope.row.Severity === 'CRITICAL' }">
                    {{ scope.row.Severity }}
                </el-tag>
            </template>
        </el-table-column>
        <el-table-column prop="URL" label="URL" />
        <el-table-column label="Operate" width="120px">
            <template #default="scope">
                <el-tooltip content="查看详情">
                    <el-button :icon="Reading" type="primary" link
                        @click="detailDialog = true; selectedRow = scope.row" />
                </el-tooltip>
                <el-tooltip content="删除">
                    <el-button :icon="Delete" link @click="deleteVuln(scope.row)" />
                </el-tooltip>
            </template>
        </el-table-column>
        <template #empty>
            <el-empty />
        </template>
    </el-table>
    <div class="flex-between mt-5px">
        <div></div>
        <el-pagination size="small" background @size-change="vp.ctrl.handleSizeChange"
            @current-change="vp.ctrl.handleCurrentChange" :pager-count="5"
            :current-page="vp.table.currentPage" :page-sizes="[10, 20, 50, 100, 200]"
            :page-size="vp.table.pageSize" layout="total, sizes, prev, pager, next"
            :total="vp.table.result.length">
        </el-pagination>
    </div>
</template>

